/*
 *  Copyrigh 2009 Codeinn
 *  This program inputStream distributed under the terms of the GNU General Public License
 *
 *  This file inputStream part of Codeinn.
 *
 *  Codeinn inputStream free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Codeinn inputStream distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Codeinn.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package codeinn;

import form.VideoForm;
import form.WaitForm;
import http.DJangoConnection;
import http.runnable.FetchAvailableVideos;
import http.runnable.FetchGroups;
import http.runnable.FetchPosts;
import http.runnable.FetchThreads;
import images.ImageCreator;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import media.Video;
import org.netbeans.microedition.lcdui.LoginScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;
import util.Dictionary;

public class CodeinnMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private DJangoConnection dJangoConnection = new DJangoConnection();
    private FetchAvailableVideos fetchAvailableVideos = new FetchAvailableVideos(this, this.dJangoConnection);
    private FetchGroups fetchGroups = new FetchGroups(this, this.dJangoConnection);
    private FetchThreads fetchThreads = new FetchThreads(this, this.dJangoConnection);
    private FetchPosts fetchPosts = new FetchPosts(this, this.dJangoConnection);
    private http.runnable.FetchVideo fetchVideo = new http.runnable.FetchVideo(this);

    private String currentUser;
    private String currentCourse;
    private String currentThread;

    public static final String MIDLET_RESPONSE_NULL = "nobody";

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command okCommand;
    private Command exitLogin;
    private Command exitWelcome;
    private Command backCommand;
    private VideoForm videoForm;
    private WaitForm waitForm;
    private Form formPosts;
    private List listPosts;
    private LoginScreen loginScreen;
    private Form formWelcome;
    private StringItem stringItem;
    private StringItem stringItemWelcome;
    private List listMain;
    private List listVideos;
    private List listGroups;
    private List listThreads;
    private Image image1;
    private SimpleCancellableTask task;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The CodeinnMIDlet constructor.
     */
    public CodeinnMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getLoginScreen());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == formPosts) {//GEN-BEGIN:|7-commandAction|1|143-preAction
            if (command == backCommand) {//GEN-END:|7-commandAction|1|143-preAction
                // write pre-action user code here
                this.getFormPosts().deleteAll();
                switchDisplayable(null, getListThreads());//GEN-LINE:|7-commandAction|2|143-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|62-preAction
        } else if (displayable == formWelcome) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|3|62-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|62-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|5|71-preAction
                // write pre-action user code here
                switchDisplayable(null, getListMain());//GEN-LINE:|7-commandAction|6|71-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|119-preAction
        } else if (displayable == listGroups) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|7|119-preAction
                // write pre-action user code here
                listGroupsAction();//GEN-LINE:|7-commandAction|8|119-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|9|122-preAction
                // write pre-action user code here
                switchDisplayable(null, getListMain());//GEN-LINE:|7-commandAction|10|122-postAction
                // write post-action user code here
                this.getListGroups().deleteAll();
            }//GEN-BEGIN:|7-commandAction|11|69-preAction
        } else if (displayable == listMain) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|11|69-preAction
                // write pre-action user code here
                listMainAction();//GEN-LINE:|7-commandAction|12|69-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|13|114-preAction
                // write pre-action user code here
                switchDisplayable(null, getFormWelcome());//GEN-LINE:|7-commandAction|14|114-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|130-preAction
        } else if (displayable == listPosts) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|15|130-preAction
                // write pre-action user code here
                listPostsAction();//GEN-LINE:|7-commandAction|16|130-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|17|132-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|18|132-postAction
                // write post-action user code here
                this.getListPosts().deleteAll();
            }//GEN-BEGIN:|7-commandAction|19|125-preAction
        } else if (displayable == listThreads) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|19|125-preAction
                // write pre-action user code here
                listThreadsAction();//GEN-LINE:|7-commandAction|20|125-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|21|127-preAction
                // write pre-action user code here
                switchDisplayable(null, getListGroups());//GEN-LINE:|7-commandAction|22|127-postAction
                // write post-action user code here
                this.getListThreads().deleteAll();
            }//GEN-BEGIN:|7-commandAction|23|82-preAction
        } else if (displayable == listVideos) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|23|82-preAction
                // write pre-action user code here
                listVideosAction();//GEN-LINE:|7-commandAction|24|82-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|25|110-preAction
                // write pre-action user code here
                switchDisplayable(null, getListMain());//GEN-LINE:|7-commandAction|26|110-postAction
                // write post-action user code here
                this.getListVideos().deleteAll();
            }//GEN-BEGIN:|7-commandAction|27|29-preAction
        } else if (displayable == loginScreen) {
            if (command == LoginScreen.LOGIN_COMMAND) {//GEN-END:|7-commandAction|27|29-preAction
                // write pre-action user code here
                String url = this.getAppProperty("url")+this.getAppProperty("url.loginMobile");
                http.runnable.Login loginThread = new http.runnable.Login(this, url, this.loginScreen.getUsername(), this.loginScreen.getPassword());
                Thread thread = new Thread(loginThread);
                thread.start();
                this.getWaitForm().setDescription(this.getAppProperty("ticker.login"), "Authenticating with the server");
                switchDisplayable(null, getWaitForm());//GEN-LINE:|7-commandAction|28|29-postAction
                //write post-action user code here
            } else if (command == exitLogin) {//GEN-LINE:|7-commandAction|29|36-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|30|36-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|31|138-preAction
        } else if (displayable == videoForm) {
            if (command == backCommand) {//GEN-END:|7-commandAction|31|138-preAction
                // write pre-action user code here
                switchDisplayable(null, getListVideos());//GEN-LINE:|7-commandAction|32|138-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|33|140-preAction
        } else if (displayable == waitForm) {
            if (command == backCommand) {//GEN-END:|7-commandAction|33|140-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|34|140-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|35|7-postCommandAction
        }//GEN-END:|7-commandAction|35|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|36|
    //</editor-fold>//GEN-END:|7-commandAction|36|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.BACK, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|24-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: loginScreen ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of loginScreen component.
     * @return the initialized component instance
     */
    public LoginScreen getLoginScreen() {
        if (loginScreen == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            loginScreen = new LoginScreen(getDisplay());//GEN-BEGIN:|27-getter|1|27-postInit
            loginScreen.setLabelTexts("Username", "Password");
            loginScreen.setTitle("loginScreen");
            loginScreen.addCommand(getExitLogin());
            loginScreen.addCommand(LoginScreen.LOGIN_COMMAND);
            loginScreen.setCommandListener(this);
            loginScreen.setBGColor(-3355444);
            loginScreen.setFGColor(0);
            loginScreen.setPassword(this.getAppProperty("user.password"));
            loginScreen.setUsername(this.getAppProperty("user.login"));
            loginScreen.setUseLoginButton(false);
            loginScreen.setLoginButtonText("Login");//GEN-END:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return loginScreen;
    }
    //</editor-fold>//GEN-END:|27-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitLogin ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of exitLogin component.
     * @return the initialized component instance
     */
    public Command getExitLogin() {
        if (exitLogin == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            exitLogin = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return exitLogin;
    }
    //</editor-fold>//GEN-END:|35-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: task ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of task component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getTask() {
        if (task == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            task = new SimpleCancellableTask();//GEN-BEGIN:|43-getter|1|43-execute
            task.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {//GEN-END:|43-getter|1|43-execute
                    // write task-execution user code here
                }//GEN-BEGIN:|43-getter|2|43-postInit
            });//GEN-END:|43-getter|2|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|3|
        return task;
    }
    //</editor-fold>//GEN-END:|43-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitWelcome ">//GEN-BEGIN:|51-getter|0|51-preInit
    /**
     * Returns an initiliazed instance of exitWelcome component.
     * @return the initialized component instance
     */
    public Command getExitWelcome() {
        if (exitWelcome == null) {//GEN-END:|51-getter|0|51-preInit
            // write pre-init user code here
            exitWelcome = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|51-getter|1|51-postInit
            // write post-init user code here
        }//GEN-BEGIN:|51-getter|2|
        return exitWelcome;
    }
    //</editor-fold>//GEN-END:|51-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formWelcome ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of formWelcome component.
     * @return the initialized component instance
     */
    public Form getFormWelcome() {
        if (formWelcome == null) {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            formWelcome = new Form("Mango", new Item[] { getStringItemWelcome(), getStringItem() });//GEN-BEGIN:|61-getter|1|61-postInit
            formWelcome.addCommand(getExitCommand());
            formWelcome.addCommand(getOkCommand());
            formWelcome.setCommandListener(this);//GEN-END:|61-getter|1|61-postInit
            // write post-init user code here
        }//GEN-BEGIN:|61-getter|2|
        return formWelcome;
    }
    //</editor-fold>//GEN-END:|61-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemWelcome ">//GEN-BEGIN:|65-getter|0|65-preInit
    /**
     * Returns an initiliazed instance of stringItemWelcome component.
     * @return the initialized component instance
     */
    public StringItem getStringItemWelcome() {
        if (stringItemWelcome == null) {//GEN-END:|65-getter|0|65-preInit
            // write pre-init user code here
            stringItemWelcome = new StringItem("Welcome", null);//GEN-LINE:|65-getter|1|65-postInit
            // write post-init user code here
        }//GEN-BEGIN:|65-getter|2|
        return stringItemWelcome;
    }
    //</editor-fold>//GEN-END:|65-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: listMain ">//GEN-BEGIN:|67-getter|0|67-preInit
    /**
     * Returns an initiliazed instance of listMain component.
     * @return the initialized component instance
     */
    public List getListMain() {
        if (listMain == null) {//GEN-END:|67-getter|0|67-preInit
            // write pre-init user code here
            listMain = new List("Mango", Choice.IMPLICIT);//GEN-BEGIN:|67-getter|1|67-postInit
            listMain.append("Videos", ImageCreator.getImage("/images/videos.png"));
            listMain.append("Groups", ImageCreator.getImage("/images/group.png"));
            listMain.addCommand(getBackCommand());
            listMain.setCommandListener(this);
            listMain.setSelectedFlags(new boolean[] { false, false });//GEN-END:|67-getter|1|67-postInit
            // write post-init user code here
        }//GEN-BEGIN:|67-getter|2|
        return listMain;
    }
    //</editor-fold>//GEN-END:|67-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listMainAction ">//GEN-BEGIN:|67-action|0|67-preAction
    /**
     * Performs an action assigned to the selected list element in the listMain component.
     */
    public void listMainAction() {//GEN-END:|67-action|0|67-preAction
        // enter pre-action user code here
        String __selectedString = getListMain().getString(getListMain().getSelectedIndex());//GEN-BEGIN:|67-action|1|73-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Videos")) {//GEN-END:|67-action|1|73-preAction
                // write pre-action user code here
                Thread thread = new Thread(this.fetchAvailableVideos);
                thread.start();
                switchDisplayable(null, getListVideos());//GEN-LINE:|67-action|2|73-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Groups")) {//GEN-LINE:|67-action|3|74-preAction
                // write pre-action user code here
                Thread thread = new Thread(this.fetchGroups);
                thread.start();
                switchDisplayable(null, getListGroups());//GEN-LINE:|67-action|4|74-postAction
                // write post-action user code here
            }//GEN-BEGIN:|67-action|5|67-postAction
        }//GEN-END:|67-action|5|67-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|67-action|6|
    //</editor-fold>//GEN-END:|67-action|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: listVideos ">//GEN-BEGIN:|81-getter|0|81-preInit
    /**
     * Returns an initiliazed instance of listVideos component.
     * @return the initialized component instance
     */
    public List getListVideos() {
        if (listVideos == null) {//GEN-END:|81-getter|0|81-preInit
            // write pre-init user code here
            listVideos = new List("available videos", Choice.IMPLICIT);//GEN-BEGIN:|81-getter|1|81-postInit
            listVideos.addCommand(getBackCommand());
            listVideos.setCommandListener(this);//GEN-END:|81-getter|1|81-postInit
            // write post-init user code here
        }//GEN-BEGIN:|81-getter|2|
        return listVideos;
    }
    //</editor-fold>//GEN-END:|81-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listVideosAction ">//GEN-BEGIN:|81-action|0|81-preAction
    /**
     * Performs an action assigned to the selected list element in the listVideos component.
     */
    public void listVideosAction() {//GEN-END:|81-action|0|81-preAction
        // enter pre-action user code here
        String __selectedString = getListVideos().getString(getListVideos().getSelectedIndex());//GEN-LINE:|81-action|1|81-postAction
        // enter post-action user code here
        /**
         * This block of code will check which video should be displayed
         */
        if (__selectedString != null) {
            Dictionary availableTitles = this.fetchAvailableVideos.getAvailableTitles();
            // When video==nobody it means that
            // a group name or a video.noavailable message was clicked
            if(availableTitles.get(__selectedString) != CodeinnMIDlet.MIDLET_RESPONSE_NULL) {
                Video video = (Video)availableTitles.get(__selectedString);
                String videoURL = video.getUrl();
                String videoDesc = video.getDescription();
                System.out.println(__selectedString+" -> "+videoDesc+" -> "+videoURL);
                // start to download the video
                this.fetchVideo.setVideoURL(videoURL);
                Thread thread = new Thread(this.fetchVideo);
                thread.start();

                // switch to wait form
                WaitForm wForm = this.getWaitForm();
                wForm.setTitle(__selectedString);
                wForm.setDescription(this.getAppProperty("ticker.loadingvideo"), videoDesc);
                switchDisplayable(null, wForm);
            }
        }
    }//GEN-BEGIN:|81-action|2|
    //</editor-fold>//GEN-END:|81-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|102-getter|0|102-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|102-getter|0|102-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|102-getter|1|102-postInit
            // write post-init user code here
        }//GEN-BEGIN:|102-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|102-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: listGroups ">//GEN-BEGIN:|118-getter|0|118-preInit
    /**
     * Returns an initiliazed instance of listGroups component.
     * @return the initialized component instance
     */
    public List getListGroups() {
        if (listGroups == null) {//GEN-END:|118-getter|0|118-preInit
            // write pre-init user code here
            listGroups = new List("Your groups", Choice.IMPLICIT);//GEN-BEGIN:|118-getter|1|118-postInit
            listGroups.addCommand(getBackCommand());
            listGroups.setCommandListener(this);//GEN-END:|118-getter|1|118-postInit
            // write post-init user code here
        }//GEN-BEGIN:|118-getter|2|
        return listGroups;
    }
    //</editor-fold>//GEN-END:|118-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listGroupsAction ">//GEN-BEGIN:|118-action|0|118-preAction
    /**
     * Performs an action assigned to the selected list element in the listGroups component.
     */
    public void listGroupsAction() {//GEN-END:|118-action|0|118-preAction
        // enter pre-action user code here
        String __selectedString = getListGroups().getString(getListGroups().getSelectedIndex());//GEN-LINE:|118-action|1|118-postAction
        // enter post-action user code here
        // __selectedString inputStream the name of the group
        if (__selectedString != null) {
            this.setCurrentCourse(__selectedString);
            Thread thread = new Thread(this.fetchThreads);
            thread.start();
            this.getListThreads().setTitle("Threads for "+__selectedString);
            switchDisplayable(null, this.getListThreads());
        }
    }//GEN-BEGIN:|118-action|2|
    //</editor-fold>//GEN-END:|118-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: listThreads ">//GEN-BEGIN:|124-getter|0|124-preInit
    /**
     * Returns an initiliazed instance of listThreads component.
     * @return the initialized component instance
     */
    public List getListThreads() {
        if (listThreads == null) {//GEN-END:|124-getter|0|124-preInit
            // write pre-init user code here
            listThreads = new List("list", Choice.IMPLICIT);//GEN-BEGIN:|124-getter|1|124-postInit
            listThreads.addCommand(getBackCommand());
            listThreads.setCommandListener(this);//GEN-END:|124-getter|1|124-postInit
            // write post-init user code here
        }//GEN-BEGIN:|124-getter|2|
        return listThreads;
    }
    //</editor-fold>//GEN-END:|124-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listThreadsAction ">//GEN-BEGIN:|124-action|0|124-preAction
    /**
     * Performs an action assigned to the selected list element in the listThreads component.
     */
    public void listThreadsAction() {//GEN-END:|124-action|0|124-preAction
        // enter pre-action user code here
        String __selectedString = getListThreads().getString(getListThreads().getSelectedIndex());//GEN-LINE:|124-action|1|124-postAction
        // enter post-action user code here
        if (__selectedString != null) {
            this.setCurrentThread(__selectedString);
            Thread thread = new Thread(this.fetchPosts);
            thread.start();
            this.getFormPosts().setTitle("Posts of "+__selectedString);
            switchDisplayable(null, this.getFormPosts());
        }
    }//GEN-BEGIN:|124-action|2|
    //</editor-fold>//GEN-END:|124-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: listPosts ">//GEN-BEGIN:|129-getter|0|129-preInit
    /**
     * Returns an initiliazed instance of listPosts component.
     * @return the initialized component instance
     */
    public List getListPosts() {
        if (listPosts == null) {//GEN-END:|129-getter|0|129-preInit
            // write pre-init user code here
            listPosts = new List("list", Choice.IMPLICIT);//GEN-BEGIN:|129-getter|1|129-postInit
            listPosts.addCommand(getBackCommand());
            listPosts.setCommandListener(this);//GEN-END:|129-getter|1|129-postInit
            // write post-init user code here
        }//GEN-BEGIN:|129-getter|2|
        return listPosts;
    }
    //</editor-fold>//GEN-END:|129-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listPostsAction ">//GEN-BEGIN:|129-action|0|129-preAction
    /**
     * Performs an action assigned to the selected list element in the listPosts component.
     */
    public void listPostsAction() {//GEN-END:|129-action|0|129-preAction
        // enter pre-action user code here
        String __selectedString = getListPosts().getString(getListPosts().getSelectedIndex());//GEN-LINE:|129-action|1|129-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|129-action|2|
    //</editor-fold>//GEN-END:|129-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">//GEN-BEGIN:|134-getter|0|134-preInit
    /**
     * Returns an initiliazed instance of image1 component.
     * @return the initialized component instance
     */
    public Image getImage1() {
        if (image1 == null) {//GEN-END:|134-getter|0|134-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|134-getter|1|134-@java.io.IOException
                image1 = Image.createImage("/video.png");
            } catch (java.io.IOException e) {//GEN-END:|134-getter|1|134-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|134-getter|2|134-postInit
            // write post-init user code here
        }//GEN-BEGIN:|134-getter|3|
        return image1;
    }
    //</editor-fold>//GEN-END:|134-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|135-getter|0|135-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|135-getter|0|135-preInit
            // write pre-init user code here
            stringItem = new StringItem("News:", "Se cancelan las clases a todos los niveles en todo el pa\u00EDs a causa de epidemia de influenza.");//GEN-LINE:|135-getter|1|135-postInit
            // write post-init user code here
        }//GEN-BEGIN:|135-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|135-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: waitForm ">//GEN-BEGIN:|136-getter|0|136-preInit
    /**
     * Returns an initiliazed instance of waitForm component.
     * @return the initialized component instance
     */
    public WaitForm getWaitForm() {
        if (waitForm == null) {//GEN-END:|136-getter|0|136-preInit
            // write pre-init user code here
            waitForm = new WaitForm("Please wait");//GEN-BEGIN:|136-getter|1|136-postInit
            waitForm.setTitle("waitForm");
            waitForm.addCommand(getBackCommand());
            waitForm.setCommandListener(this);//GEN-END:|136-getter|1|136-postInit
            // write post-init user code here
        }//GEN-BEGIN:|136-getter|2|
        return waitForm;
    }
    //</editor-fold>//GEN-END:|136-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: videoForm ">//GEN-BEGIN:|137-getter|0|137-preInit
    /**
     * Returns an initiliazed instance of videoForm component.
     * @return the initialized component instance
     */
    public VideoForm getVideoForm() {
        if (videoForm == null) {//GEN-END:|137-getter|0|137-preInit
            // write pre-init user code here
            videoForm = new VideoForm("Playing video");//GEN-BEGIN:|137-getter|1|137-postInit
            videoForm.setTitle("videoForm");
            videoForm.addCommand(getBackCommand());
            videoForm.setCommandListener(this);//GEN-END:|137-getter|1|137-postInit
            // write post-init user code here
        }//GEN-BEGIN:|137-getter|2|
        return videoForm;
    }
    //</editor-fold>//GEN-END:|137-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formPosts ">//GEN-BEGIN:|142-getter|0|142-preInit
    /**
     * Returns an initiliazed instance of formPosts component.
     * @return the initialized component instance
     */
    public Form getFormPosts() {
        if (formPosts == null) {//GEN-END:|142-getter|0|142-preInit
            // write pre-init user code here
            formPosts = new Form("form");//GEN-BEGIN:|142-getter|1|142-postInit
            formPosts.addCommand(getBackCommand());
            formPosts.setCommandListener(this);//GEN-END:|142-getter|1|142-postInit
            // write post-init user code here
        }//GEN-BEGIN:|142-getter|2|
        return formPosts;
    }
    //</editor-fold>//GEN-END:|142-getter|2|

     /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet inputStream started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet inputStream paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    public String getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(String userName) {
        this.currentUser = userName;
    }

    public String getCurrentCourse() {
        return currentCourse;
    }

    public void setCurrentCourse(String currentGroup) {
        this.currentCourse = currentGroup;
    }

    public String getCurrentThread() {
        return currentThread;
    }

    public void setCurrentThread(String currentThread) {
        this.currentThread = currentThread;
    }
    
}
